package me.obstsalatt.guildera2.core;

/**
 * Created with IntelliJ IDEA.
 * User: Robin
 * Date: 14.02.14, 17:24
 * Project: GuildEra2
 * Package: me.obstsalatt.guildera2.core
 * Descriprion:
 *
    Rechtesystem innerhalb der Gilde basiert auf Ja/Nein Prinzip und werden bitweise gespeichert
    KannFeindErklären KannGildenbank KannKicken KannInviten KannPromoten Leader(kann alles + ränge erstellen)
    value = 6bits = 0-63
 *
 */
public class GuildRank {
    private boolean[] granted_rights;
    private int value;
    private String name;


    GuildRank() {
        granted_rights = new boolean[GuildRights.MAX_RIGHT_ATTRIBUTES];
        for(int i = 0; i < granted_rights.length; i++) {
            granted_rights[i] = false;
        }
        value = 0;
        name = "default";
    }

    GuildRank(String name) {
        this();
        this.name = name;
    }

    GuildRank(String name, int[] rights) {
        this(name);
        for(int r : rights) {
            if(!granted_rights[((int)(Math.log(r)/Math.log(2)))]) {
                value += r;
            }
            granted_rights[((int)(Math.log(r)/Math.log(2)))] = true;
        }
    }

    public boolean hasRight(int right) {
        return granted_rights[((int)(Math.log(right)/Math.log(2)))];
    }

    public boolean hasRights(int[] rights) {
        for(int r : rights) {
            if(!hasRight(r)) {
                return false;
            }
        }
        return true;
    }

    public void grantRight(int r) {
        if(!hasRight(r)) {
            value += r;
            granted_rights[((int)(Math.log(r)/Math.log(2)))] = true;
        }
    }

    public void revokeRight(int r) {
        if(hasRight(r)) {
            value -= r;
            granted_rights[((int)(Math.log(r)/Math.log(2)))] = false;
        }
    }

    public String getName() {
        return name;
    }

    public int getValue() {
        return value;
    }

    public String toString() {
        return (name + "#" + value);
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        GuildRank guildRank = (GuildRank) o;

        if (!name.equals(guildRank.name)) return false;

        return true;
    }

    public int hashCode() {
        return name.hashCode();
    }
}
